<?php

global $_GPC;
global $_W;
$shopid = check_right(0);
if (!$shopid) {
    message('请先选择需要管理的店铺', $this->createWebUrl('shop'), 'error');
}
$shop = pdo_fetch('SELECT * FROM ' . tablename('j_hmoney_group') . ' WHERE weid = \'' . $_W['uniacid'] . '\' and id=\'' . $shopid . '\'');
$operation = !empty($_GPC['op']) ? $_GPC['op'] : 'categorydisplay';
$cfg = $this->module['config'];
if ($operation == 'display') {
    $category = pdo_fetchall('SELECT * FROM ' . tablename('j_hmoney_timecardcategory') . ' WHERE weid = \'' . $_W['uniacid'] . '\' and groupid =\'' . $shopid . '\' order by id desc');
    $categoryAry = array();
    foreach ($category as $row) {
        $categoryAry[$row['id']] = $row['title'];
    }
    $list = pdo_fetchall('SELECT * FROM ' . tablename('j_hmoney_projects') . ' WHERE weid=\'' . $_W['uniacid'] . '\' and groupid=\'' . $shopid . '\' order by pcate desc,id desc ');
    $objary = array();
    foreach ($list as $row) {
        $objary[$row['id']] = $row['title'];
    }
    $pindex = max(1, intval($_GPC['page']));
    $psize = 10;
    $condition = ' and a.groupid =\'' . $shopid . '\' ';
    if (isset($_GPC['keyword'])) {
        switch ($_GPC['stype']) {
            case 1:
            case 2:
                break;
        }
    }
    if ($_GPC['cid']) {
        $condition .= ' and a.pcate =\'' . $_GPC['cid'] . '\' ';
    }
    $list = pdo_fetchall('SELECT a.*,b.realname,b.cardno,b.wxcardno FROM ' . tablename('j_hmoney_timecard') . ' a left join ' . tablename('j_hmoney_membercard') . ' b on a.memberid=b.id WHERE a.weid = \'' . $_W['uniacid'] . '\' ' . $condition . ' order by a.id desc  LIMIT ' . ($pindex - 1) * $psize . ',' . $psize);
    $condition2 = str_replace('a.', '', $condition);
    $total = pdo_fetchcolumn('SELECT COUNT(*) FROM ' . tablename('j_hmoney_timecard') . ' WHERE weid = \'' . $_W['uniacid'] . '\' ' . $condition2 . ' ');
    $pager = pagination($total, $pindex, $psize);
} else {
    if ($operation == 'categorydisplay') {
        $category = pdo_fetchall('SELECT * FROM ' . tablename('j_hmoney_timecardcategory') . ' WHERE weid = \'' . $_W['uniacid'] . '\' and groupid =\'' . $shopid . '\' order by id desc');
        $list = pdo_fetchall('SELECT * FROM ' . tablename('j_hmoney_projects') . ' WHERE weid=\'' . $_W['uniacid'] . '\' and groupid=\'' . $shopid . '\' order by pcate desc,id desc ');
        $objary = array();
        foreach ($list as $row) {
            $objary[$row['id']] = $row['title'];
        }
    } else {
        if ($operation == 'categorypost') {
            $category = pdo_fetchall('SELECT * FROM ' . tablename('j_hmoney_projectscate') . ' WHERE weid=\'' . $_W['uniacid'] . '\' and groupid=\'' . $shopid . '\' order by id desc ');
            $list = pdo_fetchall('SELECT * FROM ' . tablename('j_hmoney_projects') . ' WHERE weid=\'' . $_W['uniacid'] . '\' and groupid=\'' . $shopid . '\' order by pcate desc,id desc ');
            $objary = array();
            foreach ($list as $row) {
                $objary[$row['id']] = $row['title'];
            }
            $id = $_GPC['id'];
            if ($id) {
                $item = pdo_fetch('SELECT * FROM ' . tablename('j_hmoney_timecardcategory') . ' WHERE id = :id ', array(':id' => $id));
                $content = json_decode($item['content'], true);
            }
            if (checksubmit('submit')) {
                $content = $_GPC['content'];
                $tempary = explode(',', $content);
                $tempinsert = array();
                foreach ($tempary as $row) {
                    $temp = explode('#', $row);
                    $tempinsert[$temp[0]] = $temp[1];
                }
                $data = array('weid' => $_W['uniacid'], 'groupid' => $shopid, 'title' => $_GPC['title'], 'description' => $_GPC['description'], 'price' => $_GPC['price'], 'performance' => bcmul($_GPC['performance'], 100), 'deadline' => intval($_GPC['deadline']), 'content' => json_encode($tempinsert), 'createtime' => TIMESTAMP);
                if (!$id) {
                    pdo_insert('j_hmoney_timecardcategory', $data);
                    $id = pdo_insertid();
                } else {
                    pdo_update('j_hmoney_timecardcategory', $data, array('id' => $id));
                }
                message('修改完成', $this->createWebUrl('timecard', array('op' => 'categorypost', 'id' => $id)), 'success');
            }
        } else {
            if ($operation == 'categorydelete') {
                $id = intval($_GPC['id']);
                if ($id) {
                    pdo_delete('j_hmoney_timecardcategory', array('id' => $id));
                }
                message('删除成功', $this->createWebUrl('timecard'), 'success');
            } else {
                if ($operation == 'getonemember') {
                    $keyword = trim($_GPC['keyword']);
                    if (!$keyword) {
                        exit(json_encode(array('success' => false, 'msg' => '请输入关键字')));
                    }
                    $item = pdo_fetch('SELECT a.*,b.credit1,b.credit2 FROM ' . tablename('j_hmoney_membercard') . ' a left join ' . tablename('mc_members') . ' b on a.uid=b.uid WHERE a.weid = \'' . $_W['uniacid'] . '\' and (a.realname like \'%' . $keyword . '%\' or a.cardno like \'%' . $keyword . '%\' or a.wxcardno like \'%' . $keyword . '%\' or a.mobile like \'%' . $keyword . '%\') order by a.id  desc ');
                    if (!$item) {
                        exit(json_encode(array('success' => false, 'msg' => '无此会员')));
                    }
                    exit(json_encode(array('success' => true, 'member' => $item)));
                } else {
                    if ($operation == 'gettimecardcate') {
                        $id = intval($_GPC['id']);
                        if (!$id) {
                            exit(json_encode(array('success' => false, 'msg' => 'ID不能为空')));
                        }
                        $item = pdo_fetch('SELECT * FROM ' . tablename('j_hmoney_timecardcategory') . ' WHERE id = :id ', array(':id' => $id));
                        if (!$item) {
                            exit(json_encode(array('success' => false, 'msg' => '无此卡')));
                        }
                        exit(json_encode(array('success' => true, 'item' => $item)));
                    } else {
                        if ($operation == 'getstaff') {
                            $keyword = trim($_GPC['keyword']);
                            if ($keyword) {
                                $list = pdo_fetchall('SELECT * FROM ' . tablename('j_hmoney_user') . ' WHERE weid=\'' . $_W['uniacid'] . '\' and pcate=\'' . $shopid . '\' and status=1 and ( realname like \'%' . $keyword . '%\' or useracount like \'%' . $keyword . '%\' ) order by id desc ');
                            }
                            array_push($list, array('id' => 0, 'realname' => '无', 'useracount' => '公司'));
                            exit(json_encode(array('success' => true, 'list' => $list)));
                        } else {
                            if ($operation == 'selltimecard') {
                                $memberid = $_GPC['memberid'];
                                $cardid = $_GPC['cardid'];
                                $fee = bcmul($_GPC['fee'], 100);
                                $sid = $_GPC['sid'];
                                $paytype = $_GPC['paytype'];
                                $performance = $_GPC['performance'];
                                $item = pdo_fetch('SELECT * FROM ' . tablename('j_hmoney_timecardcategory') . ' WHERE id = :id ', array(':id' => $cardid));
                                $maxnum = pdo_fetchcolumn('SELECT max(serialnum) FROM ' . tablename('j_hmoney_timecard') . ' WHERE pcate = :a ', array(':a' => $cardid));
                                $timecardno = intval($maxnum) + 1;
                                if ($item['deadline']) {
                                } else {
                                }
                                $data = array('weid' => $_W['uniacid'], 'groupid' => $shopid, 'title' => $item['title'], 'timecardno' => $timecardno, 'serialnum' => $timecardno, 'memberid' => $memberid, 'pcate' => $cardid, 'total_fee' => $paytype == -1 ? 0 : $fee, 'paytype' => $paytype, 'content' => $item['content'], 'sid' => $sid, 'performance' => $paytype == -1 ? 0 : ($sid ? $performance : 0), 'createtime' => TIMESTAMP, 'endtime' => strtotime(date('Y-m-d', strtotime('+' . $item['deadline'] . ' day')) . ' 23:59:59'), 'status' => '1');
                                pdo_insert('j_hmoney_timecard', $data);
                                $cardno = pdo_insertid();
                                $content = json_decode($item['content']);
                                $cateAry = array();
                                $category = pdo_fetchall('SELECT * FROM ' . tablename('j_hmoney_projects') . ' WHERE weid=\'' . $_W['uniacid'] . '\' and groupid=\'' . $shopid . '\' order by id desc ');
                                foreach ($category as $row) {
                                    $cateAry[$row['id']] = $row['title'];
                                }
                                foreach ($content as $index => $row) {
                                    $data2 = array('weid' => $_W['uniacid'], 'groupid' => $shopid, 'title' => $cateAry[$index], 'tid' => $cardno, 'memberid' => $memberid, 'pcate' => $cardid, 'pid' => $index, 'createtime' => TIMESTAMP, 'endtime' => $data['endtime'], 'status' => "\\\\60");
                                    $i = 0;
                                    while ($i < $row) {
                                        pdo_insert('j_hmoney_timecardorder', $data2);
                                        $i = $i + 1;
                                    }
                                }
                                exit(json_encode(array('success' => true, 'cardno' => $cardno)));
                            } else {
                                if ($operation == 'timecarddelete') {
                                    $id = intval($_GPC['id']);
                                    if ($id) {
                                        $item = pdo_fetch('SELECT * FROM ' . tablename('j_hmoney_timecard') . ' WHERE id = :id ', array(':id' => $id));
                                        if ($item) {
                                            pdo_delete('j_hmoney_timecard', array('id' => $id));
                                            pdo_delete('j_hmoney_timecardorder', array('tid' => $id));
                                        }
                                    }
                                    message('删除成功', $this->createWebUrl('timecard', array('op' => 'display')), 'success');
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}
include $this->template('web/' . $cfg['floder'] . '/timecard');